package ml.practicum.learn;

import java.util.ArrayList;
import java.util.List;

import ml.practicum.feature.LVFeature;
import ml.practicum.feature.Feature.FeatureRole;
import ml.practicum.logistic.BasicLV;
import ml.practicum.table.HeadedTable;
/**
 * factory to create perceptron fitting a certain data structure
 * @author Joscha
 *
 * @param <V> the value type the constructed perceptron should use
 */
public class PerceptronFactory <V extends Number> {

	/**
	 * default constructor
	 */
	public PerceptronFactory() {
		super();
	}
	
	/**
	 * creates a basic perceptron that fit's on the data
	 * @param data the data that the perceptron needs to fit
	 * @return the perceptron
	 */
	public BasicPerceptron<V> create(HeadedTable<LVFeature<String>,String> data){

		return new BasicPerceptron<V>(getInputs(data.getHeader(),data.getRow(0)).size());
	}
	
	/**
	 * preprocesses 1 row of inputs on the data 
	 * @param header header with processing information
	 * @param row sample row
	 * @return returns the preprocessed inputs
	 */
	private List<BasicLV> getInputs
	(
			List<LVFeature<String>> header,
			List<String> row
	)
	{
		List<BasicLV> result = new ArrayList<BasicLV>();
		for(int i =0; i<row.size();i++ ){
			if(header.get(i).getRole() == FeatureRole.INPUT){
				result.addAll(header.get(i).processValue(row.get(i)));
			}
		}
		return result;
	}
	
}
